#pragma once
#include "Client.h"
template <typename T>
class Queue {
private:
	T* elements;
	int capacity;
	int size;
	int head;
	int tail;
	int tag;
public:
	Queue(int initcapacity = 10) :capacity(initcapacity), head(0), tail(0), tag(0), size(0) {
		elements = new T[capacity];
	}
	~Queue() {
	}
	void push(const T& item);		//入队
	bool pop();						//出队
	bool top(T& t);					//队首
	bool isempty();					//判断空
	int length();					//长度
	int getSize();					//大小
	int getCapacity();				//容量
	void resize();					//扩容

};
// 大小
template <typename T>
int Queue<T>::getSize() {
	return this->size;
}
// 容量
template <typename T>
int Queue<T>::getCapacity() {
	return this->capacity;
}
// 扩容
template <typename T>
void Queue<T>::resize() {
	T* newElements = new T[2 * this->getCapacity()];
	int i = 0;
	for (int i = 0; i < this->size; i++) {
		newElements[i] = elements[head];
		head = (head + 1) % this->getCapacity();
	}
	this->head = 0;
	this->tail = this->size;
	this->tag = 0;
	this->capacity = 2 * this->getCapacity();
	T* temp = this->elements;
	this->elements = newElements;
	newElements = temp;
	delete[]newElements;
}
// 入队
template <typename T>
void Queue<T>::push(const T& item) {
	if (this->head == this->tail && tag == 1) {
		this->resize();
	}
	elements[tail] = item;
	tail = (tail + 1) % capacity;
	this->tag = 1;
	size++;
}
// 出队
template <typename T>
bool Queue<T>::pop() {
	if (this->isempty()) return false;
	head = (head + 1) % capacity;
	size--;
	this->tag = 0;
	return true;
}
// 获取队首元素
template<typename T>
bool Queue<T>::top(T& t) {
	if (this->isempty()) return false;
	t = this->elements[head];
	return true;
}
// 判断空
template<typename T>
bool Queue<T>::isempty() {
	if (this->head == this->tail && this->tag == 0) return true;
	return false;
}
// 长度
template<typename T>
int Queue<T>::length() {
	return this->tail - this->head - 1;
}



/*
#pragma once

template <typename T>
class Queue {
private:
	T* elements;
	int capacity;
	int size;
	int head;
	int tail;
public:
	Queue(int initcapacity = 10) :capacity(initcapacity), head(0), tail(-1) {
		elements = new T[capacity];
	}
	~Queue() {
		//delete[]elements;
	}
	void push(const T& item);
	void pop();
	T top();
	bool empty();
	int length();
	int getSize();
	int getCapacity();
	void resize();
	void initHead();
	void initTail();
};

template <typename T>
int Queue<T>::getSize() {
	return this->size;
}
template <typename T>
int Queue<T>::getCapacity() {
	return this->capacity;
}
template <typename T>
void Queue<T>::initHead() {
	this->head = 0;
}
template <typename T>
void Queue<T>::initTail() {
	this->tail = this->getSize() - 1;
}
template <typename T>
void Queue<T>::resize() {
	T* newElements = new T[2 * this->getCapacity()];
	int i = 0;
	while (head != tail) {
		newElements[i++] = elements[head++];
		head %= this->getCapacity();
	}
	newElements[i] = elements[head];
	this->initHead();
	this->initTail();
	this->capacity = 2 * this->getCapacity();
	T* temp = this->elements;
	this->elements = newElements;
	newElements = temp;
	delete[]newElements;
}
template <typename T>
void Queue<T>::push(const T& item) {
	if (this->getSize() == this->getCapacity()) {
		this->resize();
	}
	tail = (tail + 1) % capacity;
	elements[tail] = item;
	size++;
}
template <typename T>
void Queue<T>::pop() {
	head = (head + 1) % capacity;
	size--;
}
template<typename T>
T Queue<T>::top() {
	return this->elements[head];
}
template<typename T>
bool Queue<T>::empty() {
	return this->tail - this->head < 0 ? true : false;
}
template<typename T>
int Queue<T>::length() {
	return this->tail - this->head - 1;
}
*/